﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 绩效管理系统.Utility;

namespace 绩效管理系统.Models
{
    public class Users
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public string Sex { get; set; }
        public string PassWord { get; set; }
        public string BaseTypeId { get; set; }
        public bool IsDel { get; set; }


        public static List<Users> ListAll()
        {
            List<Users> users = new List<Users>();
            DataTable dt = Sqlhelper.ExecuteTable(
                "SELECT u.Id,u.UserName,u.BaseTypeId,u.Sex,u.IsDel,u.PassWord " +
                "FROM Users u "
                );
            foreach (DataRow dr in dt.Rows)
            {
                users.Add(dr.DataRowToModel<Users>());
            }
            return users;
        }
        public static int Insert(Users users)
        {
           return Sqlhelper.ExecuteNonQuery($"INSERT INTO Users(UserName,PassWord,Sex,BaseTypeId,IsDel) VALUES" + 
                "(@UserName, @PassWord, @Sex, @BaseTypeId, @IsDel)",
                new SqlParameter("@UserName",users.UserName),
                new SqlParameter("@PassWord", users.PassWord),
                new SqlParameter("@Sex", users.Sex),
                new SqlParameter("@BaseTypeId", users.BaseTypeId),
                new SqlParameter("@IsDel", users.IsDel)
                );
        }
        public static int Update(Users users)
        {
            return Sqlhelper.ExecuteNonQuery($"UPDATE Users SET PassWord=@PassWord,UserName=@UserName,Sex=@Sex,BaseTypeId=@BaseTypeId,IsDel=@IsDel WHERE Id=@Id",
                 new SqlParameter("@Id", users.Id),
                 new SqlParameter("@UserName", users.UserName),
                 new SqlParameter("@PassWord", users.PassWord),
                 new SqlParameter("@Sex", users.Sex),
                 new SqlParameter("@BaseTypeId", users.BaseTypeId),
                 new SqlParameter("@IsDel", users.IsDel)
                 );
        }
    }
}
